package com.gmu.controller;

import com.gmu.domain.ResResult;
import com.gmu.mapper.TblUnitsMapper;
import com.gmu.pojo.TblUnits;
import com.gmu.pojo.excel.UnitsData;
import com.gmu.pojo.vo.units.SchoolVo;
import com.gmu.service.TblUnitsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 单位表 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2022-07-24
 */
@Api(tags = "单位管理API")
@RestController
@RequestMapping("/gmu/tbl-units")
public class TblUnitsController {

    @Autowired
    private TblUnitsMapper tblUnitsMapper;

    @Resource
    private TblUnitsService tblUnitsService;

    @ApiOperation(value = "选择采样方和被采样方")
    @PostMapping("/getAllUnitsByIsSample/{sampleStatus}")
    public ResResult<?> getAllUnitsByIsSample(@PathVariable Integer sampleStatus){
        List<TblUnits> tblUnits = tblUnitsMapper.getAllUnitsByIsSample(sampleStatus);
        return new ResResult<>(200,tblUnits);
    }

    @ApiOperation(value = "通过父单位id获取其子单位")
    @GetMapping("/getUnitsByParentId/{parentId}")
    private ResResult<?> getUnitsByParentId(@PathVariable Integer parentId){
        List<TblUnits> tblUnits = tblUnitsMapper.getUnitsSchoolByParentId(parentId);
        return new ResResult<>(200,tblUnits);
    }

    //获取所有学校分类
    @ApiOperation(value = "获取所有学校单位")
    @GetMapping("/getAllSchool/{roleId}")
    public ResResult<?> getAllSchool(@PathVariable Integer roleId){
        List<SchoolVo> allSchool = tblUnitsService.getAllSchool(roleId);
        return new ResResult<>(200,allSchool);
    }

    //获取树形所有单位
    @ApiOperation(value = "获取树形所有单位")
    @GetMapping("/getAllUnits/{beSampleUnitsId}")
    public ResResult<?> getAllUnits(@PathVariable Integer beSampleUnitsId){
        TblUnits tblUnits = tblUnitsMapper.selectById(beSampleUnitsId);
        List<TblUnits> allUnits = tblUnitsMapper.getAllUnits(tblUnits.getParentId());
        return new ResResult<>(200,allUnits);
    }

    //添加单位
    @ApiOperation(value = "添加单位")
    @PostMapping("/addUnits")
    public ResResult<?> addUnits(@RequestBody TblUnits tblUnits){
        tblUnitsMapper.addUnits(tblUnits);//返回的result会自动对tblUnits进行修改
        if (1 == tblUnits.getResult()){
            return new ResResult<>(200,"添加成功");
        }
        return new ResResult<>(500,"添加失败");
    }

    //删除单位
    @ApiOperation(value = "删除单位")
    @DeleteMapping("/deleteUnits/{unitsId}")
    public ResResult<?> deleteUnits(@PathVariable Integer unitsId){
        TblUnits tblUnits = new TblUnits();
        tblUnits.setUnitsId(unitsId);
        tblUnitsMapper.deleteUnits(tblUnits);//返回的result会自动对tblUnits进行修改
        if (-2 == tblUnits.getResult()){
            return new ResResult<>(500,"该单位下存在子单位,删除失败");
        }
        if (-1 == tblUnits.getResult()){
            return new ResResult<>(500,"该单位下存在用户,删除失败");
        }
        if (1 == tblUnits.getResult()){
            return new ResResult<>(200,"删除成功");
        }
        return new ResResult<>(500,"删除失败");
    }

    //修改单位
    @ApiOperation(value = "修改单位")
    @PutMapping("updateUnits")
    public ResResult<?> updateUnits(@RequestBody TblUnits tblUnits){
        if ((tblUnitsMapper.updateById(tblUnits)) < 1){
            return new ResResult<>(500,"修改失败");
        }
        return new ResResult<>(200,"修改成功");
    }

    //导入层级关系表
    @ApiOperation(value = "导入层级关系表")
    @PostMapping("/importUnits")
    public ResResult<?> importUnits(MultipartFile file){
        return tblUnitsService.importUnits(file);
    }


}

